package objekte;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import hauptklassen.Main;

public class Civilian {
	public double x,y;
	private double vy;
	private String color = "";
	public boolean free;
	
	private BufferedImage Auto;//Das Bild des Autos
	
	private int xGraphicSize = 100;
	private int yGraphicSize = 150;
	
	
	
	public Civilian(double x, double vy){
		this.x = x*120+10;
		if (x>=(int)(Main.m.Streifenzahl/2)){
			this.vy = -vy;
		}else{
			this.vy = vy;
		}
		this.y = setTopOrButtom((int)x);
		this.color = chooseColor();
		
		//Bild laden
		try {
			this.Auto = ImageIO.read(new File("bilder/Auto_" + this.color + ".png")); //ubuntu
		} catch (IOException e) {
			try{
				this.Auto =  ImageIO.read(new File("bilder\\Auto_" + this.color + ".png")); //Windows
			}catch (Exception ex){
				e.printStackTrace();
			}
		}
	}
	public void update(){
		this.y = this.y + this.vy - Main.m.player.vy;
		if (hitPlayer()){
			if (Math.abs(this.vy)+Math.abs(Main.m.player.vy)>100){
				Main.m.endGame();
			}else{
				this.vy = -Main.m.player.vy;
				this.y = this.y + this.vy - Main.m.player.vy;
				Main.m.player.vy = -this.vy;
			}
		}
	}
	private String chooseColor(){
		int r = (int)(Math.random()*Main.Colors.length-1);
		return Main.Colors[r];
	}
	private double setTopOrButtom(int x){
		if (x<(int)(Main.m.Streifenzahl/2)){
			this.y = -this.xGraphicSize;
		}else{
			if (this.vy<Main.m.player.vy){
				this.y = Main.m.getHeight();
			}
			else{
				this.y = -this.xGraphicSize;
			}
		}
		
		return y;
	}
	public void draw(Graphics g){
		g.drawImage(this.Auto,(int)this.x,(int)this.y,xGraphicSize,yGraphicSize,null);
	}
	private boolean hitPlayer(){
		if (this.y<=Main.m.player.y+Main.m.player.yGraphicSize&&this.y+this.yGraphicSize>=Main.m.player.y
				&&this.x<=Main.m.player.x + Main.m.player.xGraphicSize&&this.x + this.xGraphicSize>=Main.m.player.x){
			return true;
		}
		return false;
	}
}
